
//form表单转换为json格式的对象
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$.fn.toJsonString = function(){
    return JSON.stringify(this.serializeObject());
};
//校验身份证
function IsIdCard(numberTmp) {
    var number = numberTmp;
    var date, Ai;
    var verify = "10X98765432";
    var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    var area = ['', '', '', '', '', '', '', '', '', '', '', '北京', '天津', '河北', '山西', '内蒙古', '', '', '', '', '', '辽宁', '吉林', '黑龙江', '', '', '', '', '', '', '', '上海', '江苏', '浙江', '安微', '福建', '江西', '山东', '', '', '', '河南', '湖北', '湖南', '广东', '广西', '海南', '', '', '', '重庆', '四川', '贵州', '云南', '西藏', '', '', '', '', '', '', '陕西', '甘肃', '青海', '宁夏', '新疆', '', '', '', '', '', '台湾', '', '', '', '', '', '', '', '', '', '香港', '澳门', '', '', '', '', '', '', '', '', '国外'];
    var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);
    if (re == null)
        return false;
    if (re[1] >= area.length || area[re[1]] == "")
        return false;
    if (re[2].length == 12) {
        Ai = number.substr(0, 17);
        date = [re[9], re[10], re[11]].join("");
    }
    else {
        Ai = number.substr(0, 6) + "19" + number.substr(6);
        date = ["19" + re[4], re[5], re[6]].join("");
    }
    if (!CheckyyyyMMdd(date))
        return false;
    var sum = 0;
    for (var i = 0; i <= 16; i++) {
        sum += Ai.charAt(i) * Wi[i];
    }
    Ai += verify.charAt(sum % 11);
    return (number.length == 18 && number == Ai);
}
function CheckyyyyMMdd(dayString) {
    // 年月日检验函数
    var digit = "0123456789";
    datelist = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if (dayString.length != 8)
        return (false);
    for (i = 0; i < 8; i++) {
        if (digit.indexOf(dayString.charAt(i), 0) == -1)
            return (false);
    }
    year = dayString.substr(0, 4); // 截取年部分
    month = dayString.substr(4, 2); // 截取月部分
    date = dayString.substr(6, 2); // 截取日部分
    if (year > 2200 || year < 1900 || month > 12 || month < 1 || date > 31 ||
        date < 1)
        return (false);
    if (date > datelist[month - 1])
        return (false);

    yyyy = eval(year);
    if (month == "02") {
        if ((yyyy % 400) == 0) {
            if (date > 29)
                return (false);
        }
        else if ((yyyy % 4) == 0 && (yyyy % 100) != 0) {
            if (date > 29)
                return (false);
        }
        else {
            if (date > 28)
                return (false);
        }
    }
    return (true);
}
//通过身份证计算生日和性别
function getBirthDayAndSex(idcard) {
    if (IsIdCard(idcard)) {
        try {
            var birth = idcard.substring(6, 10) + "-" + idcard.substring(10, 12) + "-" + idcard.substring(12, 14);
            var sex = undefined;
            if (parseInt(idcard.substr(16, 1)) % 2 == 1) {
                //男
                sex = 1;
            } else {
                //女
                sex = 2;
            }
            return {"birthday": birth, "sex": sex}
        } catch (e) {
        }
    }
    return {"birthday": "", "sex": ""}
}
